﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class GameSelect : MonoBehaviour
{
    public Button button;
    public GameStart GameStart;
    public RectTransform scrollRect;
    public GameObject scrollRectO;
    // Start is called before the first frame update
    void Start()
    {
        button = transform.GetChild(0).GetComponent<Button>();
        string MrpName = PlayerPrefs.GetString("MrpName", "vmrp.mrp");
        SetButton(button, MrpName, 1);
        int i = 1;
        string path =DefineConstants.RootPath + "/sdcard/mythroad";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        DirectoryInfo root = new DirectoryInfo(path);
        FileInfo[] files = root.GetFiles();
        foreach (var f in files)
        {
            if(f.Extension==".mrp")
            {
                Button b = Instantiate(button,transform );
                SetButton(b, f.Name, f.Length);
                i++;
            }
           
        }
        //scrollRect.sizeDelta = new Vector2(scrollRect.sizeDelta.x,i*40);
        //Test();
    }

    public unsafe void Test()
    {
        byte[] st = new byte[50];
        byte[] det = new byte[50];
        sbyte* s = (sbyte*)arms.getBytePtr(st);
        sbyte* d = (sbyte*)arms.getBytePtr(det);
        arms.strcpy(s,(sbyte *)arms.StringTobyteAdrr("test测试"));
        string ss = arms.String(s);
        arms.strcpy(d, (sbyte*)arms.StringTobyteAdrr(" ma和"));
        string ddd = arms.String(d);
        arms.strcat(d, s);
        string dd= arms.String(d);
    }

    private void SetButton(Button b,string Name,long Length)
    {
        Text t = b.transform.GetChild(0).GetComponent<Text>();
        t.text = Name + "  " + Length;
        b.onClick.AddListener(() => {
            GameStart.MrpName = t.text.Split('.')[0];
            PlayerPrefs.SetString("MrpName", Name);
            GameStart.enabled = (true);
            scrollRectO.SetActive(false);
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
